package com.thoughtworks.shoutbox.client.room;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.thoughtworks.shoutbox.client.services.ServiceCallback;
import com.thoughtworks.shoutbox.client.services.ShoutingService;
import com.thoughtworks.shoutbox.client.services.ShoutingServiceAsync;

public abstract class RoomCreationView extends Composite {
	private ShoutingServiceAsync service = GWT.create(ShoutingService.class);
	private TextArea roomBox;

	public RoomCreationView() {
		roomBox = new TextArea();
		Button roomCreationButton = new Button("Create Room");
		roomCreationButton.addClickHandler(createButtonClickListener());

		initWidget(createRootPanel(roomCreationButton));
	}

	private HorizontalPanel createRootPanel(Button createButton) {
		HorizontalPanel rootPanel = new HorizontalPanel();
		rootPanel.add(new Label("New Room:"));
		rootPanel.add(roomBox);
		rootPanel.add(createButton);
		return rootPanel;
	}

	private ClickHandler createButtonClickListener() {
		return new ClickHandler() {
			public void onClick(ClickEvent event) {
				String text = roomBox.getText();
				service.createRoom(text, new ServiceCallback<Void>() {
					public void onSuccess(Void result) {
						onNewRoow();
						roomBox.setText("");
					}
				});
			}
		};
	}
	
	protected abstract void onNewRoow();
}
